
package gui;
/*
 * the class made to show the employ all the complaint he needs to take care of 
 * the he got 3 option .
 * the first is  to close the complaint
 * the second is the transfer money to the under this username account 
 * the third one is combine the first two 
 * the employ can search complaint added by him only or search by user name
 *  
 */
import java.awt.Color;
import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import Server.BlockingUser;
import Server.CancelOrder;
import Server.Compinsert;
import Server.Getuserid;
import Server.Searccomemploy;
import client.Client;
import gui.MainPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import client.Client;
import javax.swing.JRadioButton;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.JCheckBox;


public class Followcom extends JFrame {
	
	
	
	private JPanel Sub = null;
	private MainEmployee ME;
	private JButton jButtonBack = null;

	
	final public static int DEFAULT_PORT = 5555;  
    private String host = MainPanel.ip;  //  @jve:decl-index=0:
    int port=0;
	public static Client client1;
	public static Client client2;
	public static Client client3;
	private DefaultTableModel model1 = new DefaultTableModel();
	private JButton jButtonsearchbyemp = null;
	private JButton jButtonsearchbyusername = null;
	private JLabel jLabelcustomer = null;
	private JTextField jTextFieldcutomer = null;
	private JButton jButtonsearch = null;
	private JScrollPane jScrollPanecomplaint = null;
	private JTable jTablecomp = null;
	public static String[] DataToTable=new  String[20];
	static public int complaid=0;
	public static String[] serchcombyemp=null;
	public static String[] combooption={" ","close complain","give Financial compensation to customer"};
	public static String[] copmid=null;
	public static String[] closecompandgivemoney=null;
	public static String[] givethmoney=null;
	public static String[] closecomp=null;
	ArrayList resalts =null;
	ArrayList resalts2=null;
	private JLabel jLabelc = null;
	private JLabel jLabelid = null;
	private JComboBox jComboBoxid = null;
	private JLabel jLabelaction = null;
	private JRadioButton jRadioButtonclose = null;
	private JRadioButton jRadioButtonmon = null;
	int chackbox=0;
	int closcomplain=0;
	int monetarycomp=0;
	int monandclos=0;
	String givemoney=null;
	String complainid=null;
	private JRadioButton jRadioButtoncloseand = null;
	private JLabel jLabelmon = null;
	private JTextField jTextFieldmoney = null;
	private JButton jButtongooo = null;
	private JLabel jLabelBackground = null;
	int o=0;
	private JButton jButtonHelp = null;
	public Followcom() {
		super();
		
		initialize();
	}

	/**
	 * This method initializes AcademicFrame 
	 */
	private void initialize() {
		this.setSize(1000, 720);
		this.setContentPane(getSub());
	
		
	  
			
		
		this.setTitle("Follow complaint");		
		this.addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent e) {
				Client client2 = new Client(host,DEFAULT_PORT);
				client2.accept(gui.MainPanel.Userinfo);										
				System.exit(0);	
			}
		});
	}

	private JPanel getSub() {
		
		if (Sub == null) {
			ImageIcon imageBack = new ImageIcon("comp.jpg");
			jLabelBackground = new JLabel();
			jLabelBackground.setIcon(imageBack);
			jLabelBackground.setBounds(new Rectangle(1, -1, 990, 687));		
			jLabelmon = new JLabel();
			jLabelmon.setBounds(new Rectangle(340, 563, 316, 30));
			jLabelmon.setText("Enter the amount of money you want to add this client:");
			jLabelmon.setVisible(false);
			jLabelaction = new JLabel();
			jLabelaction.setBounds(new Rectangle(481, 479, 179, 30));
			jLabelaction.setText("What would you like to do?");
			jLabelid = new JLabel();
			jLabelid.setBounds(new Rectangle(254, 475, 92, 34));
			jLabelid.setText("Complaint id");
			jLabelc = new JLabel();
			jLabelc.setBounds(new Rectangle(86, 480, 137, 31));
			jLabelc.setText("choose id and action:");
			jLabelcustomer = new JLabel();
			jLabelcustomer.setBounds(new Rectangle(137, 203, 226, 34));
			jLabelcustomer.setText("ENTER CUSTOMER USER NAME HERE:");
			Sub = new JPanel();
			Sub.setLayout(null);
			Sub.setBackground(Color.CYAN);
			Sub.add(getJButtonBack(), null);
			Sub.add(getJButtonsearchbyemp(), null);
			Sub.add(getJButtonsearchbyusername(), null);
			Sub.add(jLabelcustomer, null);
			Sub.add(getJTextFieldcutomer(), null);
			Sub.add(getJButtonsearch(), null);
			Sub.add(getJScrollPanecomplaint(), null);
			Sub.add(jLabelc, null);
			Sub.add(jLabelid, null);
			Sub.add(getJComboBoxid(), null);
			Sub.add(jLabelaction, null);
			Sub.add(getJRadioButtonclose(), null);
			Sub.add(getJRadioButtonmon(), null);
			Sub.add(getJRadioButtoncloseand(), null);
			Sub.add(jLabelmon, null);
			Sub.add(getJTextFieldmoney(), null);
			Sub.add(getJButtongooo(), null);
			Sub.add(getJButtonHelp(), null);
			jLabelcustomer.setVisible(false);
			jTablecomp.setVisible(false);
			jScrollPanecomplaint.setVisible(false);
			Sub.add(jLabelBackground, null);
		
			

		}
		return Sub; 
	}

	/**
	 * This method initializes jButtonBack	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonBack() {
		if (jButtonBack == null) {
			jButtonBack = new JButton();
			jButtonBack.setBounds(new Rectangle(193, 563, 143, 68));
			jButtonBack.setText("BACK");
			jButtonBack.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					ME = new MainEmployee();
					ME.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
					ME.setSize( 1000,720 ); // set frame size
					ME.setResizable(true);
					ME.setVisible( true ); // display frame
					Followcom.this.dispose();
				    
				}
			});
		}
		return jButtonBack;
	}

	/**
	 * This method initializes jButtonsearchbyemp	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonsearchbyemp() {
		if (jButtonsearchbyemp == null) {
			jButtonsearchbyemp = new JButton();
			jButtonsearchbyemp.setBounds(new Rectangle(50, 50, 311, 103));
			jButtonsearchbyemp.setText("SEARCH COMPLAINT BY YOUR USER NAME");
			jButtonsearchbyemp.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					/*
					 * the listener takes from the string user info the details of the employ who enter the sytem
					 * and shows him in the table all the complains he opened 
					 *  
					 */
					jButtongooo.setEnabled(true);
					jComboBoxid.setEnabled(true);

					jScrollPanecomplaint.setVisible(true);
					jTablecomp.setVisible(true);
					jLabelcustomer.setVisible(false);
					jTextFieldcutomer.setVisible(false);
					jButtonsearch.setVisible(false);
					jTablecomp.setVisible(true);
					ArrayList resalts2 = new ArrayList<String[]>();
					serchcombyemp=new String[2];
					serchcombyemp[0]="serchcompbyemploy";
					serchcombyemp[1]=MainPanel.Userinfo[3];
					client1 = new Client(host,DEFAULT_PORT);
					client1.accept(serchcombyemp);
					Object p = client1.getMsg();
					resalts2 = (ArrayList)p;
					model1.setRowCount(0);
					jComboBoxid.removeAll();
					for (int i=0;i<=(resalts2.size()-1);i++)
					{
						String[] DataToTable = (String[])resalts2.get(i);
						model1.insertRow(i, DataToTable);
						jComboBoxid.addItem(DataToTable[0]);
					
					
					}
				
					
				}	
				
			});
		}
		
		return jButtonsearchbyemp;
	}

	/**
	 * This method initializes jButtonsearchbyusername	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonsearchbyusername() {
		if (jButtonsearchbyusername == null) {
			jButtonsearchbyusername = new JButton();
			jButtonsearchbyusername.setBounds(new Rectangle(620, 50, 311, 103));
			jButtonsearchbyusername.setText("SEARCH COMPLAINT BY CUSTOMER USER NAME");
			jButtonsearchbyusername.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					jLabelcustomer.setVisible(true);
					jTextFieldcutomer.setVisible(true);
					jButtonsearch.setVisible(true);
				}
			});
		}
		return jButtonsearchbyusername;
	}

	/**
	 * This method initializes jTextFieldcutomer	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldcutomer() {
		if (jTextFieldcutomer == null) {
			jTextFieldcutomer = new JTextField();
			jTextFieldcutomer.setBounds(new Rectangle(407, 203, 205, 35));
			jTextFieldcutomer.setVisible(false);
		}
		return jTextFieldcutomer;
	}

	/**
	 * This method initializes jButtonsearch	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonsearch() {
		if (jButtonsearch == null) {
			jButtonsearch = new JButton();
			jButtonsearch.setBounds(new Rectangle(622, 203, 112, 33));
			jButtonsearch.setText("SEARCH");
			jButtonsearch.setVisible(false);

			
			jButtonsearch.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					/*
					 * the listener takes from the user name all the complains added to this user
					 * and shows them on the table
					 *
					 */
					jScrollPanecomplaint.setVisible(true);	
					jTablecomp.setVisible(true);
					jButtongooo.setEnabled(true);
					jComboBoxid.setEnabled(true);
					ArrayList resalts1 = new ArrayList<String[]>();
					serchcombyemp=new String[3];
					serchcombyemp[1]=jTextFieldcutomer.getText();
					serchcombyemp[0]="serchcompbycustomer";
					client2 = new Client(host,DEFAULT_PORT);
					client2.accept(serchcombyemp);
					
					Object p = client2.getMsg();
					resalts1 = (ArrayList)p;
					model1.setRowCount(0);
					jComboBoxid.removeAll();
					for (int i=0;(i<=resalts1.size()-1);i++)
					{
						String[] DataToTable = (String[]) resalts1.get(i);
						model1.insertRow(i, DataToTable);
						jComboBoxid.addItem(DataToTable[0]);
					
					}
					
					
					
										
				}	
				
			});
		}
		
		return jButtonsearch;
	}

	/**
	 * This method initializes jScrollPanecomplaint	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPanecomplaint() {
		if (jScrollPanecomplaint == null) {
			jScrollPanecomplaint = new JScrollPane();
			jScrollPanecomplaint.setBounds(new Rectangle(80, 253, 820, 182));
			jScrollPanecomplaint.setViewportView(getJTablecomp());
			
		}
		return jScrollPanecomplaint;
	}

	/**
	 * This method initializes jTablecomp	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJTablecomp() {
		if (jTablecomp == null) {
			jTablecomp = new JTable(model1);
			model1.addColumn("ID");
			
			model1.addColumn("COMPLAINT DESCRIPTION");
			model1.addColumn("CUSTOMER NAME");
			model1.addColumn("NAME OF EMPLOY");
			model1.addColumn("COMPLAINT STATE");
			model1.addColumn("TIME OF COMPLAINT ");
			jTablecomp.setBounds(new Rectangle(31, 330, 512, 118));
			
			jTablecomp.setVisible(false);
		
		}
		
		return jTablecomp;
	}

	/**
	 * This method initializes jComboBoxid	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBoxid() {
		if (jComboBoxid == null) {
			jComboBoxid = new JComboBox();
			jComboBoxid.setBounds(new Rectangle(362, 479, 87,29));
			jComboBoxid.setEnabled(false);
			
		
		}
		return jComboBoxid;
	}

	/**
	 * This method initializes jRadioButtonclose	
	 * 	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getJRadioButtonclose() {
		if (jRadioButtonclose == null) {
			jRadioButtonclose = new JRadioButton();
			jRadioButtonclose.setBounds(new Rectangle(673, 455, 133, 22));
			jRadioButtonclose.setText("Close complaint");
			
			jRadioButtonclose.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					/*
					 * only one radio button can be choosen each time
					 * and this is the algorithm that ensure only one choosen 
					 *  
					 */
					
					
					if(chackbox!=0){
						if(chackbox==2)
							jRadioButtonmon.doClick();
						if(chackbox==3)
							jRadioButtoncloseand.doClick();
					
				}
					jLabelmon.setVisible(false);
					jTextFieldmoney.setVisible(false);
					chackbox=1;
				}
			});
		}
		return jRadioButtonclose;
	}

	/**
	 * This method initializes jRadioButtonmon	
	 * 	
	 * @return javax.swing.JRadioButton	
	 *///Monetary compensation

	private JRadioButton getJRadioButtonmon() {
		if (jRadioButtonmon == null) {
			jRadioButtonmon = new JRadioButton();
			jRadioButtonmon.setBounds(new Rectangle(673, 480, 172, 22));
			jRadioButtonmon.setText("Monetary compensation");
			
			jRadioButtonmon.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					/*
					 * only one radio button can be choosen each time
					 * and this is the algorithm that ensure only one choosen 
					 *  
					 */
					
					jLabelmon.setVisible(true);
					jTextFieldmoney.setVisible(true);
					if(chackbox!=0){
						if(chackbox==1)
							jRadioButtonclose.doClick();
						if(chackbox==3)
							jRadioButtoncloseand.doClick();
					
				}
					chackbox=2;
					jLabelmon.setVisible(true);
					jTextFieldmoney.setVisible(true);
				}
				
			});
		}
		return jRadioButtonmon;
	}

	/**
	 * This method initializes jRadioButtoncloseand	
	 * 	
	 * @return javax.swing.JRadioButton	
	 */
	private JRadioButton getJRadioButtoncloseand() {
		if (jRadioButtoncloseand == null) {
			jRadioButtoncloseand = new JRadioButton();
			jRadioButtoncloseand.setBounds(new Rectangle(673, 505, 295, 22));
			jRadioButtoncloseand.setBounds(new Rectangle(673, 505, 288, 22));
			jRadioButtoncloseand.setText("Monetary compensation and close complaint");
			
			jRadioButtoncloseand.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					/*
					 * only one radio button can be choosen each time
					 * and this is the algorithm that ensure only one choosen 
					 *  
					 */
					
				
					if(chackbox!=0){
						if(chackbox==1)
							jRadioButtonclose.doClick();
						if(chackbox==2)
							jRadioButtonmon.doClick();
					
				}
					chackbox=3;
					jLabelmon.setVisible(true);
					jTextFieldmoney.setVisible(true);
				}
				
			});
		}
		return jRadioButtoncloseand;
	}

	/**
	 * This method initializes jTextFieldmoney	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldmoney() {
		if (jTextFieldmoney == null) {
			jTextFieldmoney = new JTextField();
			jTextFieldmoney.setBounds(new Rectangle(676, 565, 166, 27));
			jTextFieldmoney.setVisible(false);
		}
		return jTextFieldmoney;
	}

	/**
	 * This method initializes jButtongooo	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtongooo() {
		if (jButtongooo == null) {
			jButtongooo = new JButton();
			jButtongooo.setBounds(new Rectangle(406, 614, 281, 50));
			jButtongooo.setText("GO");
			jButtongooo.setEnabled(false);
			jButtongooo.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					/*
					 * once the user pressed the button the method collect the choosen radio button and(if
					 * ther is a ammount of money )and the user name 
					 * and update the data base 
					 *  
					 */
					closecompandgivemoney = new String[3];
					givethmoney=new String[3];
					closecomp=new String[3];
					if(chackbox==0){
						
						Chackempty empt = new Chackempty();
						empt.jLabelMassage.setText("you didnt choose any option");
						empt.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
						empt.setSize( 380,217 ); // set frame size
						empt.setResizable(true);
						empt.setVisible( true ); 
					}
					if(chackbox==3){
						closecompandgivemoney[0]="closecomplainadngivmon";
						givemoney=jTextFieldmoney.getText();
						int moneyvalid=0;
						if(!(isInteger(givemoney))){
							
					    	
					    
							Closecomsec close = new Closecomsec();
							close.jLabelMassage.setText("Error in the money field ");
							close.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
							close.setSize( 380,217 ); // set frame size
							close.setResizable(true);
							close.setVisible( true ); 
							 o =1;
					    		
						}
						if(0!=1){
						moneyvalid=Integer.parseInt(givemoney);
					    complainid=(String)jComboBoxid.getSelectedItem();
					    closecompandgivemoney[1]=complainid;
					    closecompandgivemoney[2]=givemoney;
					    client3 = new Client(host,DEFAULT_PORT);
						client3.accept(closecompandgivemoney);
						Object b = client3.getMsg();
						closecompandgivemoney=(String[])b;
						Closecomsec close = new Closecomsec();
						close.jLabelMassage.setText("money add & complaint colsed");
						close.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
						close.setSize( 380,217 ); // set frame size
						close.setResizable(true);
						close.setVisible( true ); 
						jTextFieldmoney.setText("");
						}
					}
					    if( chackbox==2){
					    	givethmoney[0]="givemoney";
					    	givemoney=jTextFieldmoney.getText();
					    	int moneyvalid=0;
					    	if(!(isInteger(givemoney))){
							
					    	
					    //	if(((givemoney==null)||(moneyvalid<=0))||(!(isInteger(givemoney)))){
								Closecomsec close = new Closecomsec();
								close.jLabelMassage.setText("money field is Error");
								close.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
								close.setSize( 380,217 ); // set frame size
								close.setResizable(true);
								close.setVisible( true ); 
								 o =1;
								
							//}
					    	}
					   if(o==0){
						   moneyvalid=Integer.parseInt(givemoney);
					    	givethmoney[0]="givemoney";
					    	givemoney=jTextFieldmoney.getText();
						    complainid=(String)jComboBoxid.getSelectedItem(); 
						    givethmoney[1]=complainid;
						    givethmoney[2]=givemoney;
						    client3 = new Client(host,DEFAULT_PORT);
							client3.accept(givethmoney);
							Object b = client3.getMsg();
							givethmoney=(String[])b;
							
							Closecomsec close = new Closecomsec();
							close.jLabelMassage.setText("money added to the acount");
							close.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
							close.setSize( 380,217 ); // set frame size
							close.setResizable(true);
							close.setVisible( true ); 
							jTextFieldmoney.setText("");
							
							
					   }  
					    }
					    if( chackbox==1){
					    	closecomp[0]="closecomplain";
						    complainid=(String)jComboBoxid.getSelectedItem();
						    closecomp[1]=complainid;
						    client3 = new Client(host,DEFAULT_PORT);
							client3.accept(closecomp);
							Object b = client3.getMsg();
							closecomp=(String[])b;

							Closecomsec close = new Closecomsec();
							close.jLabelMassage.setText("complaint closed");
							close.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
							close.setSize( 380,217 ); // set frame size
							close.setResizable(true);
							close.setVisible( true ); 
							jTextFieldmoney.removeAll();
						   
					    }
					    
					    
					   
					   
					    
				}
			});
		}
		return jButtongooo;
	}
	public boolean isInteger( String input )   
	{   
	   try  
	   {   
	      Integer.parseInt( input );   
	      return true;   
	   }   
	   catch( Exception e)   
	   {   
	      return false;   
	   }   
	}

	/**
	 * This method initializes jButtonHelp	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonHelp() {
		if (jButtonHelp == null) {
			jButtonHelp = new JButton();
			jButtonHelp.setBounds(new Rectangle(40, 563, 143, 68));
			jButtonHelp.setText("HELP");
			jButtonHelp.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					HelpFrame HFrame = new HelpFrame();

					HFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
					HFrame.setSize( 600,720 ); // set frame size
					HFrame.setResizable(false);
					HFrame.setVisible( true ); // display frame
				}
			});
		}
		return jButtonHelp;
	}

	
}  //  @jve:decl-index=0:visual-constraint="12,11"

